/**
 * Created by Administrator on 2018/3/12.
 */

$(function () {

    function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i].trim();
            if (c.indexOf(name) == 0) {
                return c.substring(name.length, c.length);
            }
        }
        return "";
    }

    // 开始写 jQuery 代码...
    //$("p.hh").click(function () {
    //    alert("这是一个弹出框");
    //});
    //

    $.get("/detail/" + window.location.href.split('?')[1].split('=')[1], function (data) {

        $("#title").append(JSON.stringify(data.articleName).split('"')[1]);
        editor.txt.text(JSON.stringify(data.articleContent).split('"')[1]);

    });

    $("#sub").click(function () {
        /**
         * 一般网络请求使用ajax
         * $.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});
         */
        $.ajax({
            contentType: "application/json;charset=utf8",
            type: 'POST',
            url: "/delete",
            data: JSON.stringify({
                articleId: window.location.href.split('?')[1].split('=')[1]
            }),
            success: function (data) {

                window.location.href = "/user-paper.html?userName=" + getCookie("userName");
                //window.location.href = "/user-paper.html?userName="+$("#name").val();

            },
            contentType: "application/json; charset=utf-8"
        });
    });
    $("#yes").click(function () {
        /**
         * 一般网络请求使用ajax
         * $.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});
         */


        $.ajax({
            contentType: "application/json;charset=utf8",
            type: 'POST',
            url: "/update",
            data: JSON.stringify({
                articleId: window.location.href.split('?')[1].split('=')[1],
                articleContent: editor.txt.text()
            }),
            success: function (data) {

                if (data != null) {


                    window.location.href = "/user-paper.html?userName=" + getCookie("userName");
                    //window.location.href = "/user-paper.html?userName="+$("#name").val();
                }
                else {
                    alert("更新出错！");
                }

            },
            contentType: "application/json; charset=utf-8"
        });
    });

});